package logic

import (
	"context"
	"errors"
	"register/models/mysql"
	"register/pkg"
	"register/registerrpc/internal/svc"
	"register/registerrpc/registerrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录
func (l *LoginLogic) Login(in *registerrpc.LoginCaptchaRequest) (*registerrpc.LoginCaptchaResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.UserByUsername(mysql.DB, in.UserName)
	if err != nil {
		return nil, errors.New("用户查询失败")
	}

	if user.Password != pkg.Md5(in.Password) {
		return nil, errors.New("密码不正确")
	}

	return &registerrpc.LoginCaptchaResponse{
		UserName: user.Username,
		Password: user.Password,
	}, nil
}
